package casia.isiteam.springbootshiro.service.imp;
import javax.annotation.Resource;

import casia.isiteam.springbootshiro.model.po.test2.RoleInfo;
import casia.isiteam.springbootshiro.model.po.test2.UserInfo;
import casia.isiteam.springbootshiro.model.po.test2.UserInfoRepository;
import casia.isiteam.springbootshiro.service.UserInfoService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Author wzy
 * Date 2017/7/27 21:02
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoRepository userInfoRepository;

    @Override
    public UserInfo findByUsername(String username) {
        System.out.println("UserInfoServiceImpl.findByUsername()");
        return userInfoRepository.findByUsername(username);
    }

    @Override
    public Set<String> findRoleByUsername(String username) {
        System.out.println("UserInfoServiceImpl.findRoleByUsername()");
        UserInfo userInfo  = userInfoRepository.findByUsername(username);
        if( userInfo != null ){
            Set<String> roles = new HashSet<>();
            for( RoleInfo roleInfo : userInfo.getRoles() ){
                roles.add(roleInfo.getRole());
            }
            return roles;
        }
        return Collections.emptySet();
    }
}
